package com.example.drop

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.viewModels

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.drop.adapter.PointAdapter
import com.example.drop.databinding.ActivityPointBinding
import com.example.drop.entity.PointEntity
import com.example.drop.entity.PointEntityItem
import com.example.drop.vm.PointViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path="/drop/PointActivity")
class PointActivity : BaseMvvmActivity<ActivityPointBinding>() {
    override fun getLayoutId(): Int=R.layout.activity_point
    val pointAdapter=PointAdapter()
    var pointList= listOf<PointEntityItem>()
    var type=""
    val pointViewModel:PointViewModel by viewModels ()
    override fun initView() {
        super.initView()
        //返回
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        //跳转添加
        mDataBinding.addDevice.setOnClickListener {
            ARouter.getInstance().build("/drop/PointAddActivity").navigation()
        }
        //下拉
        var arr1= mutableListOf<String>("项目名称")
        var adapter1=ArrayAdapter(this,android.R.layout.simple_spinner_item,arr1)
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDataBinding.s1.adapter=adapter1
        var arr2= mutableListOf<String>("巡检类型","安防巡检","保洁巡检","日常巡检","家居巡检")
        var adapter2=ArrayAdapter(this,android.R.layout.simple_spinner_item,arr2)
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDataBinding.s2.adapter=adapter2
        mDataBinding.s2.onItemSelectedListener=object :AdapterView.OnItemSelectedListener{
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                pointAdapter.data.clear()
                type=arr2[position]
                if (position==0){
                    pointViewModel.getPoint()
                }else{
                    pointAdapter.addData(pointList.filter { it.patrolType==type })
                }
                pointAdapter.notifyDataSetChanged()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

        }

        var arr3= mutableListOf<String>("巡检状态")
        var adapter3=ArrayAdapter(this,android.R.layout.simple_spinner_item,arr3)
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDataBinding.s3.adapter=adapter3

        //请求数据
        mDataBinding.rv.adapter=pointAdapter
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        pointViewModel.mSuccessLiveData.observe(this){
            pointAdapter.data.clear()
            pointList=it.data
            pointAdapter.addData(pointList.sortedByDescending { it.id })
            pointAdapter.notifyDataSetChanged()
        }
        pointViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }
        //点击去详情
        pointAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build( "/drop/PointXiangActivity")
                .withSerializable("point",pointAdapter.data[position])
                .navigation()
        }
    }

    override fun onResume() {
        super.onResume()
        pointViewModel.getPoint()
    }
}